package com.rainng.coursesystem.util;

import org.springframework.stereotype.Component;

@Component
public class LessonTimeConverter {
    private static final String COURSE_TIME_FORMAT = "星期%s 第%d~%d节";
    private static final String[] CHINESE_WEEK_NUMBER = new String[]
            {"零", "一", "二", "三", "四", "五", "六", "日"};

    public String covertTimePart(String time) {
        String[] split = time.split("-");
        Integer week = Integer.valueOf(split[0]);
        Integer lesson = Integer.valueOf(split[1]);
        Integer length = Integer.valueOf(split[2]);
        return String.format(COURSE_TIME_FORMAT,
                CHINESE_WEEK_NUMBER[week], lesson, lesson + length - 1);
    }

    public String revertTimePart(String formattedTime) {
        // 提取星期信息
        String weekChinese = formattedTime.substring(2, 3);
        int week = 0;
        for (int i = 0; i < CHINESE_WEEK_NUMBER.length; i++) {
            if (CHINESE_WEEK_NUMBER[i].equals(weekChinese)) {
                week = i;
                break;
            }
        }
        // 提取节次信息
        int startIndex = formattedTime.indexOf("第") + 1;
        int endIndex = formattedTime.indexOf("~");
        int startLesson = Integer.parseInt(formattedTime.substring(startIndex, endIndex));

        endIndex = formattedTime.indexOf("节");
        int endLesson = Integer.parseInt(formattedTime.substring(formattedTime.indexOf("~") + 1, endIndex));

        int length = endLesson - startLesson + 1;

        return week + "-" + startLesson + "-" + length;
    }
}
